#include "Metgine/Core/TextWriter.hpp"

namespace Metgine
{
namespace Core
{

TextWriter::TextWriter()
{

}

TextWriter::TextWriter(const std::string& path)
{

    m_stream.open(path.c_str());

}

TextWriter::~TextWriter()
{

    if (m_stream.is_open())
        m_stream.close();

}

void TextWriter::Open(const std::string& path)
{

    if (m_stream.is_open())
        m_stream.close();

    m_stream.open(path.c_str(), std::ios_base::out|std::ios_base::trunc);

}

void TextWriter::Close()
{

    if (m_stream.is_open())
        m_stream.close();

}

void TextWriter::Write(const char* format, ...)
{

    if (m_stream.is_open())
    {

        va_list args;
        va_start(args, format);

        sprintf(m_buffer, format, args);
        m_stream.write(m_buffer, strlen(m_buffer));

        m_stream.flush();

        va_end(args);

    }

}

void TextWriter::WriteLine(const char* format, ...)
{

    if (m_stream.is_open())
    {

        va_list args;
        va_start(args, format);

        vsprintf(m_buffer, format, args);
        m_stream.write(m_buffer, strlen(m_buffer));
        m_stream.write("\n", 1);

        m_stream.flush();

        va_end(args);

    }

}

}; // Core
}; // Metgine
